package com.cloudcreativity.blyg.sqlite.utils;

import android.content.Context;

import com.cloudcreativity.blyg.sqlite.Provider;
import com.cloudcreativity.blyg.sqlite.ProviderDao;

import java.util.List;

public class ProviderDaoUtils {
    private static final String TAG = ProviderDaoUtils.class.getSimpleName();
    private DaoManager mManager;

    public ProviderDaoUtils(Context context){
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    public boolean insert(Provider provider){
        boolean flag;
        flag = mManager.getDaoSession().getProviderDao().insert(provider) != -1;
        return flag;
    }

    public boolean update(Provider provider){
        boolean flag;
        flag = mManager.getDaoSession().getProviderDao().insertOrReplace(provider) != -1;
        return flag;
    }

    public void delete(Long id){
        mManager.getDaoSession().getProviderDao().deleteByKey(id);
    }

    public List<Provider> queryList(String key){
        try {
            return mManager.getDaoSession().getProviderDao().queryBuilder()
                    .where(ProviderDao.Properties.Name.like("%"+key+"%")).list();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
